<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



	<script>
        //重新加载数据
        function query(){
            datatable.ajax.reload();
        }
		function initBrandTable(){
			//初始化datatables
			return initDatatables("brandTable","/MallProductController/query.do",
					function (p) {
						//获取查询条件的值
						p.brandName = jqueryName("brandName").val();
						p.maxDate = jqueryName("maxDate").val();
						p.minDate = jqueryName("minDate").val();
						p.brandHot = jqueryName("brandHot",true).val();
						return p;
					}
					,
					[
						{
							"data": "brandId",
							"render": function (data, type, row, meta) {
								return "<input type=\"checkbox\" name=\"brandId\" value=\"" + data + "\" />";
							}
						},

						{"data": "brandName"},
						{
							"data": "brandHot",
							"render": function (data, type, row, meta) {
								console.log(data);
								if (data != null)
									return data.toString().replace("0", "否").replace("1", "是");
								else
									return "";
							}
						},
						{
							"data": "brandSort",
							"render": function (data, type, row, meta) {
								return '<input type="text" class="form-control" value="' + data + '" id="sort_' + row.brandId + '">' +
										'<button class="btn btn-primary" onclick="updateBrandSort(\'' + row.brandId + '\')"><i class="glyphicon glyphicon-refresh"></i>更新</button>';
							}
						},
						{
							"data": "brandLogo",
							"render": function (data, type, row, meta) {
								return '<img src="' + data + '" width="50px" height="50px">';
							}
						},
						{"data": "createDate"},
						{"data": "updateDate"},
						{
							"data": "brandId",
							"render": function (data, type, row, meta) {

								var v_icon = "";
								var v_text = "";
								var v_color = "";
								var v_hot = 0;

								if (row.hot == 1) {
									v_text = "非热销";
									v_icon = "glyphicon glyphicon-thumbs-down";
									v_color = "btn btn-warning";
									v_hot = 0;
								} else {
									v_text = "热销";
									v_icon = "glyphicon glyphicon-thumbs-up";
									v_color = "btn btn-success";
									v_hot = 1;
								}

								return "<div class=\"btn-group\" role=\"group\" >\n" +
										"  <button type=\"button\" class=\"btn btn-info\" onclick=\"showUpdate('" + data + "')\"><i class='glyphicon glyphicon-pencil'></i> 修改</button>\n" +
										"  <button type=\"button\" class=\"btn btn-danger\" onclick=\"deleteBrand('" + data + "')\"><i class='glyphicon glyphicon-trash'></i> 删除</button>\n" +
										"  <button type=\"button\" class=\"" + v_color + "\" onclick=\"updateBrandHot('" + data + "','" + v_hot + "')\"><i class='" + v_icon + "'></i> " + v_text + "</button>\n" +
										"</div>";
							}
						}
					]
			);
		}


        function backHtml(){
            aouDivHTML = jqueryId("aouDiv").html();
        }
        //重新加载数据
        function query(){
            datatable.ajax.reload();
        }
        //显示新增
        function showAdd()
        {

            bootBoxConfirm(
                jqueryId("aouDiv form"),
                "新增品牌",
                function(result){
                    console.log(result);

                    if(result)
                    {
                        jqueryAjax(
                            "/MallProductController/add.do",
                            {
                                brandName:jqueryName("aouBrandName").val(),
                                brandHot:jqueryName("aouBrandHot",true).val(),
                                brandSort:jqueryName("aouBrandSort").val(),
                                brandLogo:jqueryId("aouBrandLogo").val(),
                            },
                            function(result){
                                if(result.code == 0)
                                {
                                    query();//重新加载datatables
                                }
                            }
                        )
                    }


                    jqueryId("aouDiv").html(aouDivHTML);//恢复html结构
                }
            )

            initFileInput("fileImg","aouBrandLogo");//初始化上传控件


        }

        //显示修改页面
        function showUpdate(brandId){
            jqueryAjax("/MallProductController/toUpdate.do",{brandId:brandId},
                function(result){
                    console.log(result);
                    if(result.code==0){

                        jqueryName("aouBrandId").val(result.data.brandId);
                        jqueryName("aouBrandName").val(result.data.brandName);
                        jqueryName("aouBrandSort").val(result.data.brandSort);
                        jqueryName("aouBrandLogo").val(result.data.brandLogo);

                        jqueryName("aouBrandHot").each(function(){
                            if($(this).val() == result.data.brandHot){
                                $(this).prop("checked",true);
                            }
                        });

                        initFileInput("fileImg","aouBrandLogo",[result.data.brandLogo])
                        bootBoxAlert(result.msg);
                    }
                }
            )
                        bootBoxConfirm(jqueryId("aouDiv form"),"修改品牌",
                            function(type){ //确定才修改
                                if(type)
                                {
                                    jqueryAjax(
                                        "/MallProductController/updateBrand.do",
                                        {
                                            brandId:jqueryName("aouBrandId").val(),
                                            brandName:jqueryName("aouBrandName").val(),
                                            brandHot:jqueryName("aouBrandHot",true).val(),
                                            brandSort:jqueryName("aouBrandSort").val(),
                                            brandLogo:jqueryId("aouBrandLogo").val(),
                                        },
                                        function(result){
                                            if(result.code == 0)
                                            {
                                                query();//重新加载datatables
                                                bootBoxAlert(result.msg);
                                            }
                                        }
                                    )
                                  //  bootBoxAlert(result.msg);
                                }
                                jqueryId("aouDiv").html(aouDivHTML);//恢复html结构
                            }
                        )


        }

        function deleteBrand(brandId) {
            jqueryAjax("/MallProductController/deleteBrand.do", {brandId: brandId},
                function (result) {
                    console.log(result);
                    if (result.code == 0) {
                        query();//重新加载datatables
                        bootBoxAlert(result.msg);
                    }
                }
            )
        }
</script>